package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")
@RequestMapping("/api/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Operation(summary = "获取所有公告")
    @GetMapping("/list")
    public Result<List<Notice>> getAllNotices() {
        List<Notice> noticeList = noticeService.getAllNotices();
        return Result.success(noticeList);
    }

    @Operation(summary = "获取启用的公告")
    @GetMapping("/active")
    public Result<List<Notice>> getActiveNotices() {
        List<Notice> activeNotices = noticeService.getActiveNotices();
        return Result.success(activeNotices);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新公告")
    public Result<List<Notice>> getLatestNotices(@RequestParam(defaultValue = "5") Integer limit) {
        List<Notice> noticeList = noticeService.getLatestNotices(limit);
        return Result.success(noticeList);
    }

    @Operation(summary = "根据ID获取公告")
    @GetMapping("/{id}")
    public Result<Notice> getById(@PathVariable Long id) {
        Notice notice = noticeService.getById(id);
        if (notice != null) {
            return Result.success(notice);
        } else {
            return Result.error("公告不存在");
        }
    }

    @Operation(summary = "发布新公告")
    @PostMapping("/add")
    public Result<String> addNotice(@RequestBody Notice notice) {
        Boolean result = noticeService.addNotice(notice);
        if (result) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Operation(summary = "更新公告信息")
    @PutMapping("/update")
    public Result<String> updateNotice(@RequestBody Notice notice) {
        Boolean result = noticeService.updateNotice(notice);
        if (result) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @Operation(summary = "切换公告状态")
    @PutMapping("/toggle/{id}")
    public Result<String> toggleNoticeStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        Boolean result = noticeService.toggleNoticeStatus(id, isActive);
        if (result) {
            return Result.success("切换公告状态成功");
        } else {
            return Result.error("切换公告状态失败");
        }
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteNotice(@PathVariable Long id) {
        Boolean result = noticeService.deleteById(id);
        if (result) {
            return Result.success("删除公告成功");
        } else {
            return Result.error("删除公告失败");
        }
    }
}
